/**
 * Created by liuhe on 2017/05/31.
 */
('use strict');

import * as mongoose from 'mongoose';

let pageInfoSchema = new mongoose.Schema({
  siteInfo: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
    ref: 'GlobalSetup'
  }, //外链到用户信息
  tplCode: { type: String, required: true }, //页面所用模板
  platform: { type: String, required: true, enum: ['pc', 'weixin', 'app', 'pad'] }, //页面所属平台
  pageType: { type: String, required: true }, //页面类型
  pageCode: { type: String, required: true }, //页面code
  status: { type: String, required: true, enum: ['published', 'notpublished'] }, //页面状态
  updateTime: { type: Date, required: true }, //页面更新时间
  activated: { type: Boolean }, //是否激活
  shareInfo: { imgSrc: String, title: String, desc: String }, //分享信息
  isDoublePlatformTpl: { type: Boolean }, //是否是双平台
  pageStyle: { type: String }, //页面风格
  title: { type: String, default: '页面标题' }, //页面标题
  skinType: String, //一键换肤 皮肤标记
  headLv: { type: Number, enum: [0, 1, 2, 3] }, //头部显示级别
  footLv: { type: Number, enum: [0, 1, 2, 3] }, //页脚显示级别
  configOrder: { type: mongoose.Schema.Types.Mixed, required: true } //页面配置清单
});
//访问地址是固定规则生成的 用虚拟属性返回就行了
pageInfoSchema.virtual('url').get(function() {
  let url = '/';
  if (this.pageType !== 'index') {
    url = this.platform === 'pc' ? `/${this.pageType}/${this.pageCode}` : `#/${this.pageType}/${this.pageCode}`;
  }
  return url;
});
//编辑地址是固定规则生成的 用虚拟属性返回就行了
pageInfoSchema.virtual('editURL').get(function() {
  return `/mysite/${this.platform}/${this.pageType}/${this.pageCode}`;
});
pageInfoSchema.virtual('id').get(function() {
  return this._id;
});
//确保查询结果以json返回时包含虚拟字段
pageInfoSchema.set('toJSON', { getters: true, virtual: true });

export default mongoose.model('PageInfo', pageInfoSchema);
